﻿using PetaPoco;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ZRT.RHPX.Core.Data
{
    /// <summary>
    /// 基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class RepositoryBase<T> where T : class,new()
    {
        private DBContext DB { get; set; }

        #region 构造函数
        public RepositoryBase()
        {
            DB = DBContext.GetInstance();
        }
        #endregion

        /// <summary>
        /// 获取实例
        /// </summary>
        /// <param name="Id">Id</param>
        /// <returns></returns>
        public T Get(object Id)
        {
            return DB.SingleOrDefault<T>(Id);
        }

        /// <summary>
        /// 根据SQL语句 获取实例
        /// GetBySql("SELECT * FROM TEST_USER WHERE ID = @0 AND USER_NAME = @1 ","参数0的值","参数1的值")
        /// 或
        /// GetBySql("WHERE ID = @0 AND USER_NAME = @1 ","参数0的值","参数1的值")
        /// </summary>
        /// <param name="strSql"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public T GetBySql(string strSql, params object[] args)
        {
            return DB.SingleOrDefault<T>(strSql, args);
        }

        /// <summary>
        /// 返回新添加记录的主键值
        /// </summary>
        /// <param name="Entity"></param>
        /// <returns></returns>
        public object Add(T Entity)
        {
            return DB.Insert(Entity);
        }

        /// <summary>
        /// 更新对象
        /// </summary>
        /// <param name="Entity"></param>
        /// <returns></returns>
        public bool Update(T Entity)
        {
            return DB.Update(Entity) == 1;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Entity"></param>
        /// <returns></returns>
        public bool Delete(object Id)
        {
            return DB.Delete<T>(Id) == 1; ;
        }

        ///// <summary>
        ///// 删除
        ///// </summary>
        ///// <param name="sql"></param>
        ///// <param name="args"></param>
        //public void DeleteBySql(string sql, params object[] args)
        //{
        //    DB.Delete<T>(sql, args);
        //}

        /// <summary>
        /// 执行SQL
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="args"></param>
        public void Execute(string sql, params object[] args)
        {
            DB.Execute(sql, args);
        }

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="Id">Id</param>
        /// <returns></returns>
        public bool Exists(object Id)
        {
            return DB.Exists<T>(Id);
        }

        /// <summary>
        /// 查询
        /// Query("SELECT * FROM TEST_USER WHERE ID = @0 AND USER_NAME = @1 ","参数0的值","参数1的值")
        /// 或
        /// Query("WHERE ID = @0 AND USER_NAME = @1 ","参数0的值","参数1的值")
        /// </summary>
        /// <param name="strSql"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public IEnumerable<T> Query(string strSql, params object[] args)
        {
            return DB.Query<T>(strSql, args);
        }

        /// <summary>
        /// 分页查询
        /// 条件
        /// 1、不可以用select * ，需要哪一列 查询哪一列。
        /// 2、必须带 order by  排序条件
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="ItemsPerPage"></param>
        /// <param name="strSql"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public Page<T> Page(long PageIndex, long ItemsPerPage, string strSql, params object[] args)
        {
            return DB.Page<T>(PageIndex, ItemsPerPage, strSql, args);
        }

        /// <summary>
        /// 查询标量
        /// ExecuteScalar("SELECT COUNT(ID) FROM TEST_USER WHERE ID = @0 AND USER_NAME = @1 ","参数0的值","参数1的值")
        /// </summary>
        /// <param name="strSql"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public string ExecuteScalar(string strSql, params object[] args)
        {
            return DB.ExecuteScalar<string>(strSql, args);
        }

        /// <summary>
        /// 执行存储过程
        /// SqlServer:
        /// ...
        /// Oracle:
        /// 执行不带参数的存储过程：ExecuteProc("BEGIN P_TEST_GET_USER; END;");
        /// 执行带参数的存储过程：ExecuteProc("BEGIN P_TEST_GET_USER2(@0,@1); END;","6666666","地址...");
        /// </summary>
        public void ExecuteProc(string strSql, params object[] args)
        {
            DB.Execute(strSql, args);
        }
    }
}
